﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using System.Web;
using Antu.WorkFlowFactory;
using System.Data;

namespace WebSys.UI.OfficeFlow
{
    public class DepartEditSkin : MPBasicTemplate
    {
        FlowFactoryProvider _FlowFactoryProvider = (FlowFactoryProvider)ProviderService.ProviderServiceMange.GetProvider("FlowFactoryProvider");
        TextBox bmmc, bmid, sjbm, QueCon, OrderBy, orderId, txtPARENT_ID;
        Button editdata, deldata, btnQuery, submit;
        MPRepeater MPRepeater1;
        DropDownList sjbmlist;
        protected string UserId
        {
            get { if (base.Page.Session["UserId"] != null) { return base.Page.Session["UserId"].ToString(); } else { return string.Empty; } }
            set { base.Page.Session["UserId"] = value; }
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            InitControl();
            if (!Page.IsPostBack)
            {
                string depName = base.Page.Request["depName"];
                string depId = base.Page.Request["departId"];
                string ordId = base.Page.Request["orderId"];
                if (depName != "" && depId != "" && ordId != "")
                {
                    bmmc.Text = _FlowFactoryProvider.GetOneStrString("st_department", "DEPART_NAME", "DEPARTID", depId);
                    bmid.Text = depId;
                    orderId.Text = _FlowFactoryProvider.GetOneStrString("st_department", "ORDER_ID", "DEPARTID", depId);
                    OrderBy.Text = _FlowFactoryProvider.GetOneStrString("st_department", "ORDER_ID", "DEPARTID", depId);
                    txtPARENT_ID.Text = _FlowFactoryProvider.GetOneStrString("st_department", "PARENT_ID", "DEPARTID", depId);
                }
                DataTable dtlist = _FlowFactoryProvider.GetYJBM();
                if (dtlist.Rows.Count > 0)
                {
                    DataRow dr = dtlist.NewRow();
                    dr["departid"] = "0";
                    dr["depart_Name"] = "无";
                    dtlist.Rows.InsertAt(dr, 0);
                    sjbmlist.DataSource = dtlist;
                    sjbmlist.DataTextField = "depart_name";
                    sjbmlist.DataValueField = "departid";
                    sjbmlist.DataBind();
                }
                sjbmlist.SelectedValue = txtPARENT_ID.Text;
                BindData();
            }
        }


        
        private void InitControl()
        {
            MPRepeater1 = (MPRepeater)FindControl("MPRepeater1");
            sjbmlist = (DropDownList)FindControl("sjbmlist");

            bmmc = (TextBox)FindControl("bmmc");
            bmid = (TextBox)FindControl("bmid");
            sjbm = (TextBox)FindControl("sjbm");
            QueCon = (TextBox)FindControl("QueCon");
            OrderBy = (TextBox)FindControl("OrderBy");
            orderId = (TextBox)FindControl("orderId");
            txtPARENT_ID = (TextBox)FindControl("txtPARENT_ID");

            //inputdata = (Button)FindControl("inputdata");
            editdata = (Button)FindControl("editdata");
            deldata = (Button)FindControl("deldata");
            btnQuery = (Button)FindControl("btnQuery");
            submit = (Button)FindControl("submit");
            //inputdata.Click += new EventHandler(inputdata_Click);
            editdata.Click += new EventHandler(editdata_Click);
            deldata.Click += new EventHandler(deldata_Click);
            btnQuery.Click += new EventHandler(btnQuery_Click);
            submit.Click += new EventHandler(submit_Click);
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            BindData();
        }
        protected void BindData()
        {
            string depId = base.Page.Request["departId"];
            DataTable dt = _FlowFactoryProvider.GetDepartMentUser(depId, sjbmlist.SelectedValue, QueCon.Text);
            MPRepeater1.DataSource = dt;
            MPRepeater1.DataBind();
        }

        #region 插入新部门
        protected void inputdata_Click(object sender, EventArgs e)
        {
            //清空原有数据
            bmid.Text = "";
            bmmc.Text = "";
            orderId.Text = "";
            OrderBy.Text = _FlowFactoryProvider.GetOrderId(bmid.Text, sjbmlist.SelectedValue, orderId.Text);
        }
        #endregion

        #region 编辑部门信息
        protected void editdata_Click(object sender, EventArgs e)
        {
            string sjbml = sjbmlist.SelectedValue;//上级部门
            string orderId = OrderBy.Text;
            string parentId = string.Empty;
            if (!string.IsNullOrEmpty(txtPARENT_ID.Text)) { parentId = txtPARENT_ID.Text; }
            //string isOA = "0";
            //if (chkOA.Checked)
            //{
            //    isOA = "1";
            //}
            //string strByfz = cbxByfz.Checked ? "1" : "0";

            if (String.IsNullOrEmpty(bmid.Text))//新增
            {
                string departId = _FlowFactoryProvider.GetNewDptId();
                
                if (!string.IsNullOrEmpty(orderId))
                {
                    _FlowFactoryProvider.InsertDepartment(departId, bmmc.Text, sjbmlist.SelectedValue, orderId, parentId);
                    Antu.Utilities.UtilitiesPage.AlertDialog("新增成功", "window.location.href=window.location.href;window.parent['leftFrame'].location.href=window.parent['leftFrame'].location.href;");
                    bmid.Text = departId; //用新ID赋值
                }
                else
                {
                    Antu.Utilities.UtilitiesPage.AlertDialog("请检查部门名称与排序字段是否录入！", "");
                }

            }
            else//修改
            {
                string dptId = base.Page.Request["departid"];
                string OldOrderId = _FlowFactoryProvider.GetOrdIdByDptId(dptId);
                string isHaveOrderid = _FlowFactoryProvider.GetOrdIdByordId(orderId);
                if (string.IsNullOrEmpty(isHaveOrderid) || isHaveOrderid == orderId)
                {
                    _FlowFactoryProvider.SaveDepartMent(dptId, bmmc.Text, sjbml, orderId, parentId);
                    if (OldOrderId != orderId)  //如果产生了新orderid,则需更新其下级部门的orderid
                    {
                        int havecount = _FlowFactoryProvider.GetDptmentByPId(dptId);
                        if (havecount > 0)
                        {
                            int pos = OldOrderId.Length + 1;
                            _FlowFactoryProvider.UpdateOrderId(orderId, OldOrderId);
                        }
                    }
                    Antu.Utilities.UtilitiesPage.AlertDialog("修改成功", "window.location.href=window.location.href;window.parent['leftFrame'].location.href=window.parent['leftFrame'].location.href;");
                        
                }
                else
                {
                    Antu.Utilities.UtilitiesPage.AlertDialog("排序字段已经存在，请检查！", "");
                }
            }
        }
        #endregion

        #region 删除部门信息
        protected void deldata_Click(object sender, EventArgs e)
        {
            bool del1 = _FlowFactoryProvider.DeleteTableRow("st_user_department", "order_id", bmid.Text);
            bool del2 = _FlowFactoryProvider.DeleteTableRow("st_department", "departid", bmid.Text);
            Antu.Utilities.UtilitiesPage.AlertDialog("删除成功", "window.location.href=window.location.href;window.parent['leftFrame'].location.href=window.parent['leftFrame'].location.href;");
        }
        #endregion


        #region 对部门的人员进行重新设置
        protected void submit_Click(object sender, EventArgs e)
        {
            string dptId = base.Page.Request["departId"];
            for (int temp = 0; temp < MPRepeater1.Items.Count; temp++)
            {
                Label idtib = (Label)MPRepeater1.Items[temp].FindControl("idtib");
                CheckBox checkselect = (CheckBox)MPRepeater1.Items[temp].FindControl("checkselect");
                string userId=idtib.Text.Trim();
                int tt = _FlowFactoryProvider.GetUserDepNum(userId, dptId);
                if (checkselect.Checked)
                { if (tt == 0) { _FlowFactoryProvider.InsertUserDpt(userId, dptId); } }
                else { if (tt > 0) { _FlowFactoryProvider.DeleteUserDpt(userId, dptId); } }
            }
            Antu.Utilities.UtilitiesPage.RegJsScript("window.location.href=window.location.href;window.parent['leftFrame'].location.href=window.parent['leftFrame'].location.href;");
        }
        #endregion

        
        


    }
}


